<?php
namespace  Backend\Controller;
use Think\Controller;

class AdGameController extends BaseController
{
    public function index() {
        $model =D('AdGame');
        $data=$model ->get_adGame();

        $this->assign(array(
            'data' => $data['data'],
            'page' => $data['page'],
        ));

        $this->display();
    }

    public function create() {
        $data=$_POST;
        if($data) {
            $model = D('AdGame');

            if (!$data = $model->create()) {
                $this->ajaxReturn(['status' => 'failed', 'msg' => $model->getError()]);
            } else {

                if($result = $model->add()) {
                    $this->ajaxReturn(['status' => 'success', 'msg' => '添加成功！']);
                } else {
                    $this->ajaxReturn(['status' => 'failed', 'msg' =>$model->getError() ]);
                }
            }
        }

        $this->display();
    }


    public function edit() {
        $id     = I('get.id');
        $data   = $_POST;
        if($data) {
            $model = D('AdGame');
            if (!$model->create()) {
                $this->ajaxReturn(['status' => 'failed', 'msg' => $model->getError() ]);
            } else {

                if($model->save() !== FALSE) {
                    $this->ajaxReturn(['status' => 'success', 'msg' => '修改成功！']);
                } else {
                    $this->ajaxReturn(['status' => 'failed', 'msg' => $model->getError() ]);
                }
            }

        }
        $where['id']    = ['eq', $id];
        $ad_game        = D('AdGame')->where($where)->find();
        $this->assign('ad_game', $ad_game);
        $this->display();
    }


    public function delete() {
        $id = I('post.id');
        if(!$id) {
            $this->ajaxReturn(['status' => 'failed', 'msg' => '操作有误，重新选择！']);
        }
        $model = D('AdGame');
        if($model->delete($id)) {
            $this->ajaxReturn(['status' => 'success', 'msg' => '操作成功！']);
        } else {
            $this->ajaxReturn(['status' => 'failed', 'msg' =>'操作失败！']);
        }
    }
}